// pages/actAdmit002/actAdmit002.js
var projectID
const db = wx.cloud.database()
const _ = db.command

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    projectDetail: "",
    notAdmitVlnList:[""]
  },

  getUniqueProject() {
    db.collection('project')
      .where({
        _id: this.data.projectID
      })
      .get()
      .then(res => {
        console.log(res.data)
        this.setData({
          projectDetail: res.data[0],
          notAdmitVlnList: res.data[0].vlnListNotAdmit,
          limitPeoNum: res.data[0].peopleNum,
          hasAdmitVlnList: res.data[0].vlnListHasAdmit,
        })
        console.log("待录取志愿者列表", this.data.notAdmitVlnList)
        console.log("已录取志愿者列表", this.data.hasAdmitVlnList)
        console.log("限制人数", this.data.limitPeoNum)
        //用于录取完判断录取人满了没
        if (this.data.hasAdmitVlnList.length == this.data.limitPeoNum) {
          //给剩下没录取的每个人发订阅消息和清除掉用户的待录取数组里的对应项目和清空该项目待录取列表
          for (let index = 0; index < this.data.notAdmitVlnList.length; index++) {
            db.collection("all_user").where({
              _openid: this.data.notAdmitVlnList[index].openid
            }).update({
              data: {
                will_be_admit: _.pull({
                  project_id: this.data.projectID
                })
              }
            }).then(res => {
              console.log("把因录取人满而未被录取的用户的will数组里的该项目丢掉成功", res)

              wx.cloud.callFunction({
                name: "sendSubptMessage",
                data: {
                  openId: this.data.notAdmitVlnList[index].openid,
                  projectName: this.data.projectDetail.taskTitle,
                  condition: "该活动申请审核未通过",
                  userName: this.data.notAdmitVlnList[index].name,
                  teamNickname: app.globalData.teamInfo.teamInfo.teamName,
                  phoneNum: app.globalData.teamInfo.managerInfo.phone,
                  flag: "judgeProject"
                }
              }).then(res => {
                console.log("发送订阅消息云函数调用成功", res)
              })
            }).catch(err => {
              console.log("把因录取人满而未被录取的用户的will数组里的该项目丢掉失败", err)
            })
          }
          db.collection("project").doc(this.data.projectID).update({
            data: {
              vlnListNotAdmit: []
            }
          }).then(res => {
            console.log("将该项目的待录取志愿者列表清空成功", res)
            this.setData({
              notAdmitVlnList: []
            })
          }).catch(err => {
            console.log("将该项目的待录取志愿者列表清空失败", err)
          })
        }
      })
  },
  admit(e) {
    let vlnid = e.currentTarget.dataset.id
    console.log("对应申请人的_id", vlnid)
    wx.cloud.callFunction({
        name: "updateUserInfo",
        data: {
          action: "act_admit",
          openid: e.currentTarget.dataset.id,
          project_id: this.data.projectID,
          projectName: this.data.projectDetail.taskTitle
        }
      })
      .then(res => {
        console.log("把项目从will里拿掉，放进admit里", res)
      })

    //拿到项目待录取列表里对应的志愿者
    let index = this.data.notAdmitVlnList.findIndex(e => e._id == vlnid)
    console.log(this.data.notAdmitVlnList[index])
    let tempVln = this.data.notAdmitVlnList[index]
    wx.cloud.callFunction({
        name: "getProjectList",
        data: {
          flag: "act_admit",
          project_id: this.data.projectID,
          vln_id: tempVln._id,
          vln_openid: tempVln.openid,
          vln_name: tempVln.name,
          vln_avar: tempVln.avar,
        }
      })
      .then(res => {
        console.log("把will数组里的志愿者扔到has里", res)

        wx.cloud.callFunction({
          name: "sendSubptMessage",
          data: {
            openId: tempVln.openid,
            projectName: this.data.projectDetail.taskTitle,
            condition: "该活动申请审核通过",
            userName: tempVln.name,
            teamNickname: app.globalData.teamInfo.teamInfo.teamName,
            phoneNum: app.globalData.teamInfo.managerInfo.phone,
            projectId:this.data.projectId,
            flag: "judgeProject"
          }
        }).then(res => {
          console.log("发送订阅消息云函数调用成功", res)
        })

        this.getUniqueProject()
      })
  },
  refuse(e) {
    wx.cloud.callFunction({
        name: "updateUserInfo",
        data: {
          action: "act_no_admit",
          openid: e.currentTarget.dataset.id,
          project_id: this.data.projectID,
          projectName: this.data.projectDetail.taskTitle
        }
      })
      .then(res => {
        console.log("把用户will数组里的项目丢进reject", res)
      })
    wx.cloud.callFunction({
        name: "getProjectList",
        data: {
          flag: "act_no_admit",
          project_id: this.data.projectID,
          vln_id: e.currentTarget.dataset.id
        }
      })
      .then(res => {
        console.log("把notAdmit里的对应用户直接丢掉", res)

        wx.cloud.callFunction({
          name: "sendSubptMessage",
          data: {
            openId: tempVln.openid,
            projectName: this.data.projectDetail.taskTitle,
            condition: "该活动申请审核未通过",
            userName: tempVln.name,
            teamNickname: app.globalData.teamInfo.teamInfo.teamName,
            phoneNum: app.globalData.teamInfo.managerInfo.phone,
            flag: "judgeProject"
          }
        }).then(res => {
          console.log("发送订阅消息云函数调用成功", res)
        })
        this.getUniqueProject()
      })
  },
  goVlnDetail(e){
    console.log("111",e)
    wx.navigateTo({
      url: '../../memberDetail/memberDetail?id='+e.currentTarget.dataset.vlnid,
    })
  },
  onLoad: function (options) {
    console.log(options.id)
    console.log(app.globalData.teamInfo.managerInfo.phone)
    this.setData({
      projectID: options.id
    })
    this.getUniqueProject()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.cloud.callFunction({
      name: "",
      data: {

      }
    })
  },
})